package servlet;

import user.Member;
import util.DBUtilDao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class AddMoneyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = (String) request.getSession().getAttribute("username");
        if (username == null) {
            response.getWriter().println("请先登录！！！（2秒后自动跳转）");
            response.setHeader("refresh", "2;URL=login.jsp");
        }else {

            DBUtilDao dbUtilDao=new DBUtilDao();
            Member member=null;
            String phoneNumber= (String) request.getSession().getAttribute("username");
            float price=Float.parseFloat(request.getParameter("price"));
            try {
                dbUtilDao.addMoney(phoneNumber,price);
                member = dbUtilDao.getMember(phoneNumber);//根据手机号，查询数据库中的个人中心的信息
                request.getSession().setAttribute("member",member);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                dbUtilDao.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            response.getWriter().println("<h2>充值成功！！！（2秒后自动跳转）<h2>");
            response.setHeader("refresh", "2;URL=member.jsp");
        }
    }
}
